(function ($) {
	const user = app.getState();
	var keyDic,keyID,rw;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		
		today = getCurrentDeviceDay() ;//'2021-08-11'
		thisYM = today.substring(0,today.length - 3);
		currYM = thisYM;
		var ymDict = getYMDWithStr(today)
		$('#ymDate')[0].innerText = outChinaYMStr2(ymDict.year,ymDict.month)
		$('.myHeader').on('tap', '#changeYM', selectYearMonth);
		var self = plus.webview.currentWebview();
		keyID = self.keyID
		rw = self.rw
		FindApprovalSetShow()
		
	}
	// 获取表头
	function FindApprovalSetShow() {
		var arg = {
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			parr:rw +'^'+'Y'
		}
		// 获取审批设置
		var method = 'web.INMSetComm:FindApprovalSetShow'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		// console.log(JSON.stringify(arg));
		$.DHCWebService(paramDict, function(result) {
			var dataArr = result['data'];
			//console.log(JSON.stringify(dataArr));
			keyDic = dataArr;
			if(dataArr.length > 0){
				keyDic = jsonSort(dataArr,'ShowSort'); //按照后台order字段排序				
				getApprovalDetailByUser()
			}
		}, function(errorStr) {
			console.log(errorStr)
			document.querySelector('.examWrapper .mui-table-view').innerHTML = ''
			showNullTip(errorStr)
		});
	}
	function getApprovalDetailByUser(){
		var arg = {
			loginID: user.loginID,
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			keyID: keyID
		}
		// console.log(JSON.stringify(arg));
		// 获取详情
		var method = 'web.INMPlatform:FindApprovalDetailByUser'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		$.DHCWebService(paramDict, function(result) {
			var dataArr = result['data'];
			plus.nativeUI.closeWaiting()
			//console.log(JSON.stringify(dataArr))
			var examTableView = document.querySelector('.examWrapper .mui-table-view');
			var html = ''
			var oneCard = ''
			for (var j = 0; j < dataArr.length; j++) {
				// console.log(JSON.stringify(dataArr[j]))
				var inner = ''
				for (var i = 0; i < keyDic.length; i++) {
					var one = keyDic[i];
					var key = one.rwProp
					var content = '<div class="key" data-rwProp="'+ one.rwProp +'">'+ one.ShowDesc +'：</div><div class="value">'+dataArr[j][key]+'</div>'
					inner += content;
				}
				var cardBody1 = '<li class="mui-table-view-cell"><div class="content">'
				var cardBody2 = '</div><div class="agreeBtn agree" data-rw="'+dataArr[j]['rw']+'" data-arg="' +dataArr[j]['ToValue']+ '">通过</div>'+
					'<div class="agreeBtn disagree" data-rw="'+dataArr[j]['rw']+'" data-arg="' +dataArr[j]['BackToValue'] + '">驳回</div></li>';
				oneCard = cardBody1 + inner + cardBody2;//一个卡片的内容
				html += oneCard //多个卡片的内容
			}
			examTableView.innerHTML = html;
			// 驳回
			$('.examWrapper .mui-table-view-cell').on('tap', '.disagree', requestSaveApprovalData)
			
			// 通过
			$('.examWrapper .mui-table-view-cell').on('tap', '.agree', requestSaveApprovalData)
			if (html == '') {
				showNullTip('没有可以审批的数据啦')
			}else{
				hideNullTip()
			}
		}, function(errorStr) {
			console.log(errorStr)
			document.querySelector('.examWrapper .mui-table-view').innerHTML = ''
			showNullTip(errorStr)
			plus.nativeUI.closeWaiting()
		});
	}
	function requestSaveApprovalData(){
		plus.nativeUI.showWaiting('正在审批')
		var rw = this.getAttribute('data-rw')
		var value = this.getAttribute('data-arg')
		var desc = this.innerText
		var arg = {
			loginID: user.loginID,
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			keyID: keyID,
			toValue:value,
			toValueDesc:desc,
			option:desc,
			rws:rw
		}
		//console.log(JSON.stringify(arg));
		// 审批接口
		var method = 'web.INMPlatform:SaveApprovalData'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'Method'
		}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if(result.msg != '1'){
				mui.toast(result.msg)
				plus.nativeUI.closeWaiting()
			}else{//刷新页面
				$('.examWrapper .mui-table-view-cell').off('tap', '.disagree', requestSaveApprovalData)
				// 通过
				$('.examWrapper .mui-table-view-cell').off('tap', '.agree', requestSaveApprovalData)
				getApprovalDetailByUser()
			}
		}, function(errorStr) {
			console.log(errorStr)
			mui.toast(result.msg)
			plus.nativeUI.closeWaiting()
		});
	}
	
	/*
	 * 选择年月
	 */
	function selectYearMonth() {
		var _self = this;
		if(_self.picker) {
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				_self.innerText = dateStr.substring(2, dateStr.length);
				_self.picker.dispose();
				_self.picker = null;
			});
		} else {
			var optionsJson = this.getAttribute('data-options') || '{}';
			var options = JSON.parse(optionsJson);
			_self.picker = new mui.DtPicker(options);
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				_self.innerText = dateStr.substring(2, dateStr.length);
				_self.picker.dispose();
				_self.picker = null;
			});
		}
	}
	
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		// console.log(index)
		mui('.nullData')[0].style.display = 'none';
	}
})(mui);